package com.andy.wan.android.mvp.ui.fragment;

import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.andy.ui.base.BaseFragment;
import com.andy.ui.base.SimpleFragmentActivity;
import com.andy.wan.android.R;
import com.andy.wan.android.mvp.bean.LeftMenuBean;
import com.andy.wan.android.mvp.bean.LoginBean;
import com.andy.wan.android.mvp.event.LoginEvent;
import com.andy.wan.android.mvp.ui.activity.LoginActivity;
import com.andy.wan.android.mvp.ui.adapter.LeftMenuAdapter;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.SPUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Arrays;

import butterknife.BindView;

/**
 * @Author: luofan
 * @CreateDate: 2020/5/13 15:48
 * @Description: 左边菜单
 */
public class DrawerLayoutMenu extends BaseFragment {

    @BindView(R.id.tv_name)
    TextView mTvName;
    @BindView(R.id.ll_login)
    LinearLayout mLlLogin;
    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;

    private LeftMenuBean[] mLeftMenuArray = {new LeftMenuBean("收藏", R.drawable.ic_like_fill),
            new LeftMenuBean("关于", R.drawable.ic_about),
            new LeftMenuBean("退出", R.drawable.ic_logout)};

    @Override
    public int getLayoutId() {
        return R.layout.fragment_drawer_layout_menu;
    }

    @Override
    public void initLogic() {
        initLoginName();
        initRecycler();
        mLlLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String json = SPUtils.getInstance().getString(LoginBean.class.getName());
                if (TextUtils.isEmpty(json)) {
                    toLogin();
                }
            }
        });
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    private void toLogin() {
        LoginActivity.startMe(getContext());
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onLoginEvent(LoginEvent event) {
        initLoginName();
    }

    private void initLoginName() {
        String json = SPUtils.getInstance().getString(LoginBean.class.getName());
        if (TextUtils.isEmpty(json)) {
            mTvName.setText(R.string.login);
        } else {
            LoginBean loginBean = GsonUtils.fromJson(json, LoginBean.class);
            if (TextUtils.isEmpty(loginBean.getNickname())) {
                mTvName.setText(loginBean.getUsername());
            } else {
                mTvName.setText(loginBean.getNickname());
            }
        }
    }

    public void logout() {
        SPUtils.getInstance().put(LoginBean.class.getName(), "");
        getActivity().finish();
        toLogin();
    }

    private void toLikeFragment() {
        SimpleFragmentActivity.startMe(getContext(), LikeFillFragment.class.getName(), null);
    }

    private void initRecycler() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        LeftMenuAdapter menuAdapter = new LeftMenuAdapter();
        menuAdapter.setNewData(Arrays.asList(mLeftMenuArray));
        menuAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                switch (position) {
                    case 0:
                        toLikeFragment();
                        break;
                    case 1:
                        break;
                    case 2:
                        logout();
                        break;
                    default:
                }
            }
        });
        mRecyclerView.setAdapter(menuAdapter);
    }
}
